package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Sound;
import com.itheima.domain.mongo.SoundTime;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.SoundService;
import com.itheima.service.mongo.SoundTimeService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.SoundVo;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class    SoundManager {
    //private static Integer remainingTimes = 10;
    private static Integer remainingTimes;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Reference
    private SoundTimeService soundTimeService;

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private UserLikeService userLikeService;

    @Reference
    private SoundService soundService;

    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;

    public void sendSound(MultipartFile soundFile) throws IOException {
        //上传语音到fastDFS
        StorePath soundPath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(),
                FileUtil.extName(soundFile.getOriginalFilename()), null);
        String soundUrl = webServer.getWebServerUrl() + soundPath.getFullPath();
        //组装sound
        Sound sound = new Sound();
        sound.setId(ObjectId.get());
        sound.setCreated(System.currentTimeMillis());
        sound.setSoundUrl(soundUrl);
        sound.setUserId(UserHolder.get().getId());
        //调用service保存
        soundService.saveSound(sound);
    }

    public SoundVo getSound() {
        //登录用户id
        Long userId = UserHolder.get().getId();

        //调用service随即查询一条数据
        Sound sound = soundService.findSound();
        stringRedisTemplate.opsForValue().set("sound_objectId",sound.getId().toHexString());


        if (sound == null || sound.getUserId() == userId){
            throw new RuntimeException("网络繁忙~~");
        }

        if (stringRedisTemplate.hasKey("sound_time" + userId)) {
            remainingTimes = Integer.parseInt(stringRedisTemplate.opsForValue().get("sound_time" + userId));
        }
        //存入sound_time
        --remainingTimes;
        stringRedisTemplate.opsForValue().set("sound_time" + userId,remainingTimes + "");
        /*SoundTime soundTime = new SoundTime();
        //soundTime.setId(ObjectId.get());
        soundTime.setUserId(userId);
        soundTime.setCreated(System.currentTimeMillis());
        soundTime.setRemainingTimes(remainingTimes);*/
        if (remainingTimes >= 0) {
            soundTimeService.save(userId, Integer.parseInt(stringRedisTemplate.opsForValue().get("sound_time" + userId)));
        }

        SoundTime soundTime = soundTimeService.findById(userId);
        //组装vo
        SoundVo soundVo = new SoundVo();
        UserInfo userInfo = userInfoService.findById(sound.getUserId());
        BeanUtil.copyProperties(userInfo,soundVo,true);
        soundVo.setSoundUrl(sound.getSoundUrl());
        if (soundTime.getRemainingTimes() >= 0) {
            soundVo.setRemainingTimes(soundTime.getRemainingTimes());
        }

        return soundVo;
    }
}
